package com.qzsoft.zhjd.materiel.modules.materieldatainput.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataDisableDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.entity.MaterielDataEntity;
import com.qzsoft.zhjd.materiel.modules.materieldatainput.dto.MaterielDataInputDto;
import com.qzsoft.zhjd.materiel.modules.materieldatainput.service.MaterielDataInputService;

/**
 * @ClassName MaterielDataInputController
 * @Description 磅单补录控制器
 * @author linhuawei
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/api/materielDataInput")
public class MaterielDataInputController {

    @Autowired
    private MaterielDataInputService materielDataInputService;

    /**
     * @Title page
     * @Description 补录磅单分页列表
     * @param params 查询条件
     * @return 补录磅单分页列表
     */
    @RequiresPermissions("materiel:datainput:page")
    @GetMapping("/page")
    public Result<IPage<MaterielDataListDto>> page(@RequestParam Map<String, Object> params) {
        IPage<MaterielDataListDto> page = materielDataInputService.pageDto(params);
        return ResultUtils.ok(page);
    }

    /**
     * @Title info
     * @Description 获取数据对象
     * @param id 数据主键
     * @return 数据对象
     */
    @RequiresPermissions("materiel:datainput:update")
    @GetMapping("/info/{id}")
    public Result<MaterielDataEntity> info(@PathVariable("id") String id) {
        MaterielDataEntity entity = materielDataInputService.getById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "数据不存在");
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 添加数据
     * @param entity 需要添加的数据
     * @return 添加结果
     */
    @RequiresPermissions("materiel:datainput:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MaterielDataEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        materielDataInputService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改数据
     * @param entity 需要修改的数据
     * @return 修改结果
     */
    @RequiresPermissions("materiel:datainput:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody MaterielDataEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        materielDataInputService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title disable
     * @Description 磅单废弃
     * @param dto 废弃信息
     * @return 废弃结果
     */
    @RequiresPermissions("materiel:datainput:disable")
    @PostMapping("/disable")
    public Result<String> disable(@RequestBody MaterielDataDisableDto dto) {
        ValidatorUtils.validateEntity(dto);
        materielDataInputService.disable(dto);
        return ResultUtils.ok();
    }

    /**
     * @Title template
     * @Description 下载模板
     */
    @RequiresPermissions("materiel:datainput:template")
    @GetMapping("/template")
    public void template(HttpServletResponse response) {
        materielDataInputService.template(response);
    }

    /**
     * @Title importExcel
     * @Description 数据导入
     * @param deviceCode 地磅设备编号
     * @param file       导入文件
     * @return 导入结果
     */
    @RequiresPermissions("materiel:datainput:import")
    @PostMapping("/import")
    public Result<List<MaterielDataInputDto>> importExcel(String deviceCode, MultipartFile file) {
        if (ObjectUtils.isEmpty(deviceCode)) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "地磅不可为空");
        }
        if (null == file || 0 == file.getSize()) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "上传文件不可为空");
        }
        List<MaterielDataInputDto> result = materielDataInputService.importExcel(deviceCode, file);
        return ResultUtils.ok(result);
    }

    /**
     * @Title getLastYear
     * @Description 获取最新一条数据年份
     * @return 最新一条数据年份
     */
    @GetMapping("/getLastYear")
    public Result<String> getLastYear() {
        String year = materielDataInputService.getLastYear(new HashMap<String, Object>());
        return ResultUtils.ok(year);
    }
}
